﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace SmartSpace.Web
{
    /// <summary>
    /// 使用模板页的Page父类
    /// </summary>
    public class SsTemplatePage : System.Web.UI.Page
    {
        public string HostUrl { get; set; }
        public string AppUrl { get; set; }
        public SmartSpace.Core.User SsUser { get; internal set; }

        public SsTemplatePage()
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            SiteTemplate template = service.GetSiteTemplate(SsSite.Current.TemplateId);
            MasterPageFile = string.Format("~/_templates/_site/{0}/NestedPage.Master", template.Code);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                OnLoad(sender, e);
            }
            catch
            {

            }
        }

        protected virtual void OnLoad(object sender, EventArgs e)
        {
        }


        /// <summary>
        /// 注册css文件
        /// </summary>
        /// <param name="key"></param>
        /// <param name="relativePath"></param>
        public void RegisterCss(string key, string relativePath)
        {
            foreach (Control item in this.Page.Header.Controls)
            {
                if (item.ID == key)
                {
                    return;
                }
            }

            string resolvedPath = relativePath;
            if (resolvedPath.StartsWith("./"))
            {
                resolvedPath = resolvedPath.Substring(2);
            }
            else if (resolvedPath.StartsWith("//"))
            {
                resolvedPath = resolvedPath.Substring(2);
            }
            else if (resolvedPath.StartsWith("/"))
            {
                resolvedPath = resolvedPath.Substring(1);
            }

            HtmlLink link = new HtmlLink();
            link.ID = string.Format("css_{0}", key);
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            link.Attributes.Add("href", resolvedPath);
            this.Page.Header.Controls.Add(link);
        }

        /// <summary>
        /// 注册脚本
        /// </summary>
        /// <param name="key"></param>
        /// <param name="relativePath"></param>
        public void RegisterJs(string key, string relativePath)
        {
            foreach (Control item in this.Page.Header.Controls)
            {
                if (item.ID == key)
                {
                    return;
                }
            }

            string resolvedPath = relativePath;
            if (resolvedPath.StartsWith("./"))
            {
                resolvedPath = resolvedPath.Substring(2);
            }
            else if (resolvedPath.StartsWith("//"))
            {
                resolvedPath = resolvedPath.Substring(2);
            }
            else if (resolvedPath.StartsWith("/"))
            {
                resolvedPath = resolvedPath.Substring(1);
            }

            HtmlGenericControl script = new HtmlGenericControl("script");
            script.ID = string.Format("js_{0}", key);
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("src", this.Page.ResolveUrl(string.Format("~/_modules/{0}", resolvedPath)));
            this.Page.Header.Controls.Add(script);
        }
    }
}
